package com.sansi.pencilbook.controller;

import com.sansi.pencilbook.DO.ResponseDO;
import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.entity.Category;
import com.sansi.pencilbook.service.CategoryServer;
import com.sansi.pencilbook.service.UseCategoryServer;
import com.sansi.pencilbook.util.ResponseDOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 功能说明: 类别控制器
 *
 * @author lsy13
 */
@Slf4j
@RestController
@RequestMapping("/api/category")
public class CategoryController extends VerifyController implements BaseController{

    @Autowired
    private CategoryServer categoryServer;

    @Autowired
    private UseCategoryServer useCategoryServer;

    @GetMapping("/v/getFisrtCategory")
    public ResponseDO getFisrtCategory() {
        Result result = categoryServer.getFirstCategory();
        return result.isSuccess() ? ResponseDOUtil.setResponseDO(result.isSuccess(), result.getData()) :
                ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
    }

    @PostMapping("/v/getCategoryByParent")
    public ResponseDO getCategoryByParent(@RequestParam("categoryId") int categoryId) {
        Result result = categoryServer.getCategoriesByParent(categoryId);
        return result.isSuccess() ? ResponseDOUtil.setResponseDO(result.isSuccess(), result.getData()) :
                ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
    }

    @PostMapping("/v/getCategoryByName")
    public ResponseDO getCategoryByName(@RequestParam("name") String name) {
        Result result = categoryServer.getCategoryByName(name);
        return result.isSuccess() ? ResponseDOUtil.setResponseDO(result.isSuccess(), result.getData()) :
            ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
    }

    @PostMapping("/v/getCategoryById")
    public ResponseDO getCategoryById(@RequestParam("id") int id) {
        Result result = categoryServer.getCategoryById(id);
        return result.isSuccess() ? ResponseDOUtil.setResponseDO(result.isSuccess(), result.getData()) :
            ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
    }

    @PostMapping("/v/getChildrenCategoryByName")
    public ResponseDO getChildrenCategoryByName(@RequestParam("name") String name) {
        Category category = (Category) categoryServer.getCategoryByName(name).getData();
        Result result = categoryServer.getCategoriesByParent(category.getCategoryId());
        return result.isSuccess() ? ResponseDOUtil.setResponseDO(result.isSuccess(), result.getData()) :
            ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
    }

    @GetMapping("/v/getAllCategory")
    public ResponseDO getAllCategory() {
        Result result = useCategoryServer.getAllCategory();
        return result.isSuccess() ? ResponseDOUtil.setResponseDO(result.isSuccess(), result.getData()) :
                ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
    }

    @PostMapping("/a/addCategory")
    public ResponseDO addCategory(@RequestBody Category category, HttpServletRequest request) {
        if (checkAdmin(request)) {
            Result result = categoryServer.addCategory(category);
            return result.isSuccess() ? ResponseDOUtil.setResponseDO(result.isSuccess(), result.getData()) :
                    ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
        } else {
            return ResponseDOUtil.setResponseDO(false, "请登录后操作");
        }
    }

    @PostMapping("/a/modifyCategory")
    public ResponseDO modifyCategory(@RequestBody Category category, HttpServletRequest request) {
        if (checkAdmin(request)) {
            Result result = categoryServer.modifyCategory(category);
            return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
        } else {
            return ResponseDOUtil.setResponseDO(false, "请登录后操作");
        }
    }

    @PostMapping("/a/deleteCategory")
    public ResponseDO deleteCategory(@RequestBody Category category, HttpServletRequest request) {
        if (checkAdmin(request)) {
            Result result = categoryServer.deleteCategory(category);
            return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
        } else {
            return ResponseDOUtil.setResponseDO(false, "请登录后操作");
        }
    }

    @PostMapping("/a/deleteCategories")
    public ResponseDO deleteCategories(@RequestBody List<Category> categories, HttpServletRequest request) {
        if (checkAdmin(request)) {
            Result result = categoryServer.deleteCategories(categories);
            return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
        } else {
            return ResponseDOUtil.setResponseDO(false, "请登录后操作");
        }
    }
}
